﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Practices.Composite.Regions;

#region namespaces model-view

using EQual.MainModule.Projects .Views;
using EQual.MainModule.Projects .Models ;

#endregion

using EQual.Infrastructure;

namespace EQual.MainModule.Projects.Presenters
{
    class ProjectsController : EQual.Infrastructure.MVPFramework.Presenters.ControllerBase<Views .IProjectsGridView ,DataAccesLayer .Project > ,EQual.Infrastructure.IModuleController 
    {
        DataAccesLayer.ProjectsDataAccesLayer accesLayer;
        EQual.MainModule.DetailsContainer containerView = new DetailsContainer ();

        public DataAccesLayer.ProjectsDataAccesLayer AccesLayer
        {
            get { return accesLayer; }
            set { accesLayer = value; }
        }

       IRegionManager _regionManager;

       ProjectsPresentationModel  ViewModel;

       IProjectsGridView  _view_Main;

       public IProjectsGridView  View
       {
           get { return _view_Main; }
           set { _view_Main = value; }
       }

       public ProjectsController(IRegionManager regionManager):this()
       {
           this._regionManager = regionManager;
       }

       public ProjectsController()
       {
           accesLayer = new DataAccesLayer.EQualDataLayerFactory().GetProjectsAccesLayer();
       }

       //secondary Presenters and Views 
       //DetailsViews.EmployeeDetailsPresenter detailsPresenter;
       //DetailsViews.EmployeeDistributionPresenter distributionPresenter;
         

       public void Run()
       {
           ViewModel = new EQual.MainModule.Projects.Models.ProjectsPresentationModel();
           View = new EQual.MainModule.Projects.Views.ProjectsGridView();

           ViewModel.View = View;
           View.Model = ViewModel;
         
           View.ItemSelected += new EventHandler<Microsoft.Practices.Composite.Events.DataEventArgs<object >>(View_ItemSelected);
           View.ItemDeleted += new EventHandler<Microsoft.Practices.Composite.Events.DataEventArgs<object >>(View_ItemDeleted);
           View.ItemAdded += new EventHandler<Microsoft.Practices.Composite.Events.DataEventArgs<object >>(View_ItemAdded);

           //  detailsPresenter = new EQual.MainModule.Employees.DetailsViews.EmployeeDetailsPresenter();
           //   distributionPresenter = new EQual.MainModule.Employees.DetailsViews.EmployeeDistributionPresenter();
           //  preseter_details = new DocumentDetailedControlPresenter();
           //  presenter_forms = new FormsController();

           this._regionManager.RegisterViewWithRegion(RegionNames.MainRegion,() => this.View);
           this._regionManager.Regions[RegionNames.MainRegion].Activate(this.View);
       }

       void View_ItemAdded(object sender, Microsoft.Practices.Composite.Events.DataEventArgs<object > e)
       {
           RequestNewItemCreation(e.Value);
       }

       void View_ItemSelected(object sender, Microsoft.Practices.Composite.Events.DataEventArgs<object > e)
       {
           InjectViews();
           if (e.Value == null) return;

           details.ActiveItem = e.Value;
           TaskModel.ActiveProject = e.Value;
           //distributionPresenter.Dispaly(e.Value);
           //dff.DispalyDocumentDetails(e.Value);
       }

       void View_ItemDeleted(object sender, Microsoft.Practices.Composite.Events.DataEventArgs<object > e)
       {
           RequestItemDelete(e.Value);
       }

      // DetailsViews.EmployeeDetailedControl   dff = new DetailsViews.EmployeeDetailedControl ();
       DetailsViews.ProjectDetailsView details = new EQual.MainModule.Projects.DetailsViews.ProjectDetailsView();
       Tasks.Models.TaskPresenter TaskModel = new EQual.MainModule.Projects.Tasks.Models.TaskPresenter();

       protected virtual void InjectViews()
       {
           //throw in to the datails region the datails view
           IRegion region = _regionManager.Regions[RegionNames.ActionRegion];

           if (!region.Views.Contains(containerView))
           {
               containerView.AddView(GetDecoratedView(details , "Λεπτομέρειες"));
               containerView.AddView(GetDecoratedView(TaskModel.View, "Ενέργειες"));
               //  containerView.AddView(GetDecoratedView(distributionPresenter.View,"Δίανομή"));
               //  containerView.AddView(GetDecoratedView(dff, "Λεπτομέρειες"));
               region.Add(containerView);
           }

           //activate the current view
           region.Activate(containerView);
       }

       public object GetDecoratedView(object view,string header)
       {
           return new DataGridDecorator(view, header);
       }

       
       public void SubmitChanges()
       {
          
       }

       public void RequestNewItemCreation(object project)
       {
         
           AccesLayer.AddNewProject(project);
       }

       public void RequestItemDelete(object project)
       {
           AccesLayer.DeleteProject(project);
       }

       internal void RequestItemDelete(System .Collections .IList  projectList)
       {
           AccesLayer.DeleteProject(projectList);
       }

    }
}
